package Class;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileFilter;

public class Employee {
	int id;
	String name;
	String address;
	int phone;
	String mail;
	String image;
	int status;
	int decentralization;
	String loginName;
	String loginPassword;
	int question;
	String answer;
	Date dateCreated;
	
	public void setId(int id)
	{
		this.id = id;
	}
	public int getId()
	{
		return this.id;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return this.name;
	}
	
	public void setAddress(String address)
	{
		this.address = address;
	}
	public String getAddress()
	{
		return this.address;
	}
	
	public void setPhone(int phone)
	{
		this.phone = phone;
	}
	public int getPhone()
	{
		return this.phone;
	}
	
	public void setMail(String mail)
	{
		this.mail = mail;
	}
	public String getMail()
	{
		return this.mail;
	}
	public void setImage(String image)
	{
		this.image =image;
	}
	public String getImage()
	{
		return this.image;
	}
	
	public void setStatus(int status)
	{
		this.status = status;
	}
	public int getStatus()
	{
		return this.status;
	}
	
	public void setDecentralization(int decentralization)
	{
		this.decentralization = decentralization;
	}
	public int getDecentralization()
	{
		return this.decentralization;
	}
	
	public void setLoginName(String loginName)
	{
		this.loginName = loginName;
	}
	public String getLoginName()
	{
		return this.loginName;
	}
	
	public void setLoginPassword(String loginPassword)
	{
		this.loginPassword = loginPassword;
	}
	public String getLoginPassword()
	{
		return this.loginPassword;
	}
	
	public void setQuestion(int question)
	{
		this.question = question;
	}
	public int getQuestion()
	{
		return this.question;
	}
	
	public void setAnswer(String answer)
	{
		this.answer = answer;
	}
	public String getAnswer()
	{
		return this.answer;
	}
	
	public void setDateCreated(Date dateCreated)
	{
		this.dateCreated = dateCreated;
	}
	public Date getDateCreated()
	{
		return this.dateCreated;
	}
	
	public static File changeAvatar(JButton btnChangeAvatar)
	{
		FileFilter filter = new FileNameExtensionFilter("Image Files (JPG,JPEG,PNG)","jpg","jpeg","png");
        JFileChooser openFile  = new JFileChooser();
        openFile.setFileFilter(filter);
        openFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
        openFile.setAcceptAllFileFilterUsed(false);
		if(openFile.showOpenDialog(btnChangeAvatar) == JFileChooser.APPROVE_OPTION)
		{
			File fileAvatar = openFile.getSelectedFile();
			ImageIcon temp = new ImageIcon(fileAvatar.getPath());
			Image img = temp.getImage();
			Image resizedImg = img.getScaledInstance(80, 80,  java.awt.Image.SCALE_SMOOTH);
			ImageIcon avatar = new ImageIcon(resizedImg);
			btnChangeAvatar.setIcon(avatar);
			return fileAvatar;
		}
		return null;
	}
	
	public void changeMyPassword(String currentPassword,String newPassword,String confirmPassword,JLabel lbShowWarningChangePassword)
	{
		System.out.println(MD5.encryptMD5(currentPassword));
		System.out.println(loginPassword);
		if(MD5.encryptMD5(currentPassword).equals(loginPassword))
		{
			if(newPassword.equals(confirmPassword))
			{
				Procedure.changePassword(loginName, newPassword);
				loginPassword = newPassword;
				lbShowWarningChangePassword.setText("");
				JOptionPane.showMessageDialog(null, "Change password successfully!","Message",JOptionPane.PLAIN_MESSAGE);
			}
			else lbShowWarningChangePassword.setText("Cofirm new password don't same new password!");
		}
		else lbShowWarningChangePassword.setText("Invalid current password!");
	}
	
	public void setMyInfo(JLabel lbMyInfoID,JTextField tfMyInfoLoginName,JTextField tfMyInfoName,JTextArea taMyInfoAddress,JTextField tfMyInfoDateCreated,JTextField tfMyInfoPhone,JTextField tfMyInfoNameEmail,JComboBox cbMyInfoEmail,JComboBox cbMyInfoFunction,JComboBox cbMyInfoWorkingStatus,JButton btnMyInfoAvatar)
	{
		lbMyInfoID.setText(Integer.toString(this.id));
		tfMyInfoLoginName.setText(this.loginName);
		tfMyInfoName.setText(this.name);
		taMyInfoAddress.setText(this.address);
		String resultTimeCreated = new SimpleDateFormat("dd-MM-yyyy").format(dateCreated.getTime());
		tfMyInfoDateCreated.setText(resultTimeCreated);
		tfMyInfoPhone.setText(Integer.toString(this.phone));
		String showMail = this.mail.substring(0, this.mail.lastIndexOf('@'));
        String subMail = this.mail.substring(this.mail.lastIndexOf('@'), this.mail.length());  
		tfMyInfoNameEmail.setText(showMail);	                      
        if(subMail.trim().equals("@gmail.com")){
        	cbMyInfoEmail.setSelectedIndex(0);
        }
        else{
        	cbMyInfoEmail.setSelectedIndex(1);
        }
		//cbMyInfoEmail.setSelectedIndex(this.decentralization);
		cbMyInfoFunction.setSelectedIndex(this.decentralization);
		cbMyInfoWorkingStatus.setSelectedIndex(this.status);
		ImageIcon icon;
		if(this.image != null)
			icon = new ImageIcon("emps/"+this.image);
		else icon = new ImageIcon("images/anonymous.png");
		btnMyInfoAvatar.setIcon(icon);
	}
}
